package com.tyjw.common;

public class ByteUtil {
	
	/** 
     * @功能 短整型与字节的转换 
     * @param 短整型 
     * @return 两位的字节数组 
     */  
    public static byte[] shortToByte(short number) {  
        int temp = number;  
        byte[] b = new byte[2];  
        for (int i = 0; i < b.length; i++) {  
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位  
            temp = temp >> 8; // 向右移8位  
        }  
        return b;  
    } 
    
    /** 
     * @功能 字节的转换与短整型 
     * @param 两位的字节数组 
     * @return 短整型 
     */  
    public static short byteToShort(byte[] b) {  
        short s = 0;  
        short s0 =b[0]; //(short) (b[0] & 0xff);// 最低位  
        short s1 =b[1]; //(short) (b[1] & 0xff);  
        s0 <<= 8;  
        s = (short) (s0 | s1);
        return s;  
    } 
    
    /** 
     * @方法功能 整型与字节数组的转换 
     * @param 整型 
     * @return 四位的字节数组 
     */  
    public static byte[] intToByte(int i) {  
        byte[] bt = new byte[4];  
        bt[0] = (byte) (0xff & i);  
        bt[1] = (byte) ((0xff00 & i) >> 8);  
        bt[2] = (byte) ((0xff0000 & i) >> 16);  
        bt[3] = (byte) ((0xff000000 & i) >> 24);  
        return bt;  
    }  
  
    
    /** 
     * @方法功能 字节数组和整型的转换 
     * @param 字节数组 
     * @return 整型 
     */  
    public static int bytesToInt(byte[] bytes,int offset) {  
        int num = bytes[offset] & 0xFF;  
        num |= ((bytes[offset+1] << 8) & 0xFF00);  
        num |= ((bytes[offset+2] << 16) & 0xFF0000);  
        num |= ((bytes[offset+3] << 24) & 0xFF000000);  
        return num;  
    }
  
    
    /** 
     * @方法功能 字节数组和长整型的转换 
     * @param 字节数组 
     * @return 长整型 
     */  
    public static byte[] longToByte(long number) {  
        long temp = number;  
        byte[] b = new byte[8];  
        for (int i = 0; i < b.length; i++) {  
            b[i] = new Long(temp & 0xff).byteValue();  
            // 将最低位保存在最低位  
            temp = temp >> 8;  
            // 向右移8位  
        }  
        return b;  
    }  
  
    /** 
     * @方法功能 字节数组和长整型的转换 
     * @param 字节数组 
     * @return 长整型 
     */  
    public static long byteToLong(byte[] b) {  
        long s = 0;  
        long s0 = b[0] & 0xff;// 最低位  
        long s1 = b[1] & 0xff;  
        long s2 = b[2] & 0xff;  
        long s3 = b[3] & 0xff;  
        long s4 = b[4] & 0xff;// 最低位  
        long s5 = b[5] & 0xff;  
        long s6 = b[6] & 0xff;  
        long s7 = b[7] & 0xff; // s0不变  
        s1 <<= 8;  
        s2 <<= 16;  
        s3 <<= 24;  
        s4 <<= 8 * 4;  
        s5 <<= 8 * 5;  
        s6 <<= 8 * 6;  
        s7 <<= 8 * 7;  
        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;  
        return s;  
    }
    
    
    /** 
     * 将byte转换为一个长度为8的byte数组，数组每个值代表bit 
     */  
    public static byte[] getBooleanArray(byte b) {  
        byte[] array = new byte[8];  
        for (int i = 7; i >= 0; i--) {  
            array[i] = (byte)(b & 1);  
            b = (byte) (b >> 1);  
        } 
       
        return array;  
    } 
    
    /** 
     * 把byte转为字符串的bit 
     */  
    public static String byteToBit(byte b) {  
        return ""  
                + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)  
                + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)  
                + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)  
                + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);  
    } 
    
    
    /** 
     * 二进制字符串转byte 
     */  
    public static byte decodeBinaryString(String byteStr) {  
        int re, len;  
        len = byteStr.length();  
        if (len != 4 && len != 8) {  
            return 0;  
        }  
        if (len == 8) {// 8 bit处理  
            if (byteStr.charAt(0) == '0') {// 正数  
                re = Integer.parseInt(byteStr, 2);  
            } else {// 负数  
                re = Integer.parseInt(byteStr, 2) - 256;  
            }  
        } else {// 4 bit处理  
            re = Integer.parseInt(byteStr, 2);  
        }  
        return (byte) re;  
    }  
    
    
    /** 
     * 把byte[]转换成16进制进制字符串 
     * @param b 
     * @return 
     */  
    public static String bytes2HexString(byte[] b) {    
          String ret = "";    
          for (int i = 0; i < b.length; i++) {    
           String hex = Integer.toHexString(b[ i ] & 0xFF);    
           if (hex.length() == 1) {    
            hex = '0' + hex;    
           }  
           ret += hex.toUpperCase()+" ";    
          }    
          return ret;    
     }  
    
   //获取高四位
    public static int getHeight4(byte data){
    	
        int height;
        height = ((data & 0xf0) >> 4);
        return height;
    }

   //获取低四位
    public static int getLow4(byte data){
        int low;
        low = (data & 0x0f);
        return low;
    }
    
    
    // 把byte 转化为两位十六进制数  
    public static String toHex(byte b) {  
        String result = Integer.toHexString(b & 0xFF);  
        if (result.length() == 1) {  
            result = '0' + result;  
        }  
        return result;  
    }  
    
    
    public static byte[] Int2Bytes_LE(int iValue){
        byte[] rst = new byte[4];
        // 先写int的最后一个字节
        rst[0] = (byte)(iValue & 0xFF);
        // int 倒数第二个字节
        rst[1] = (byte)((iValue & 0xFF00) >> 8 );
        // int 倒数第三个字节
        rst[2] = (byte)((iValue & 0xFF0000) >> 16 );
        // int 第一个字节
        rst[3] = (byte)((iValue & 0xFF000000) >> 24 );
        return rst;
    }
    
	public  static byte[] hexStr2Bytes(String src){
		int m=0,n=0;    
	    int l=src.length()/2;
	    byte[] ret = new byte[l];    
	    for (int i = 0; i < l; i++){    
	        m=i*2+1;    
	        n=m+1;    
	        ret[i]=(byte)(Integer.valueOf(src.substring(i*2, m) + src.substring(m,n), 16) & 0xFF);    
	    }    
	    return ret;    
	}
	
    public static byte[] ipV4AddressToBytes(String ipv4Address) {
        String[] split = ipv4Address.trim().split("\\.");
        byte[] bytes = new byte[4];
        int length = split.length;
        if (length == 4) {
            for (int i = 0; i < length; i++) {
                bytes[i] = (byte) Integer.valueOf(split[i]).intValue();
            }
        } else {
            throw new RuntimeException("ipv4Address contains four byte, but found " + length + "," + ipv4Address);
        }
        return bytes;
    }
    
    
    public static byte[] hexToByte(String hex){

    	hex = hex.replace(" ","");
    	int byteLength = hex.length()/2;
    	byte[] bytes = new byte[byteLength];
    	int m = 0;
    	int n = 0;
    	for(int i = 0; i<byteLength;i++){
	    	m = i*2+1;
	    	n = m+1;
	    	int intHex = Integer.decode("0x"+hex.substring(i * 2, m) + hex.substring(m, n));
	    	bytes[i] = Byte.valueOf((byte) intHex);
    	}
    	return bytes;
    }
}
